This time, we are sharing content about springcloud service registration and discovery, which will be explained by building service centers, service registration and service discovery respectively. Now many startups in Beijing have begun to rely on springcloud. It may be because of the rich documents and components. After all, it is a relatively complete microservice architecture at present. I hope this sharing can bring good help to everyone;
Eureka Service Center
As far as I know and use a lot of registration centers include zookeeper and Eureka. My previous article shared dubbo+zookeeper to build services, so this time I use Eureka. The springcloud framework also recommends it as a registration center. Of course, it can be integrated with other service registration centers. After all, springcloud relies on springboot to build projects, so it is very fast to integrate other components. First, create the registration center project eureka_server, and introduce dependencies through the following:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId></dependency>
Then add configuration items to the application.yml file:
server: port: 2001spring: application: name: eureka-serveureureka: client: register-with-eureka: false #Forbid yourself to register as a service fetch-registry: false #Mask registration information instance: prefer-ip-address: true instance-id: ${spring.application.name}:${server.port}After the configuration is completed, you need to start the class and add annotation @EnableEurekaServer. The settings are basically completed and run. Visit http:// localhost:2001/ to get the following interface:
Provider registration service
With the service registration center, we also need to provide some services and register these services with the service center. In order to facilitate this, we first create an interface module project eureka_api that is used by service providers and consumers, and create the following interface and request return parameter entity class:
public interface UserInterface { @PostMapping("/users") MoRp<List<MoUser>> getUsers(MoRq rq); @GetMapping("/msg") String getMsg();}MoUser Entity:
public class MoUser { private long id; private String userName; private String userPwd; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserPwd() { return userPwd; } public void setUserPwd(String userPwd) { this.userPwd = userPwd; }}Then create the module eureka_provider on our service provider side, and also introduce eureka dependencies, but there is a little difference between it and the server side:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId></dependency>
Then create the service UserController to be provided by the service provider, and implement the UserInterface interface in our eureka_api module. The code is as follows:
@RestControllerpublic class UserController implements UserInterface { @Autowired private HttpServletRequest request; @Override public MoRp<List<MoUser>> getUsers(MoRq rq) { MoRp<List<MoUser>> rp = new MoRp<>(); List<MoUser> list = new ArrayList<>(); for (int i = 0; i < 5; i++) { MoUser moUser = new MoUser(); moUser.setId(i); moUser.setUserName("Shenniu" + i); list.add(moUser); } rp.setT(list); rp.setStatus(list.size() >= 1 ? 1 : 0); rp.setMessage(list.size() >= 1 ? "" : "No data yet"); return rp; } @Override public String getMsg() { return "Here is a provider, port:"+ request.getServerPort(); }}It should be noted here that there is no PostMapping or GetMapping added to the two service interfaces of the Controller, because this is declared by the implemented interface; after defining the users and msg services, we also need to be able to inject them into the service registration center, so the following configuration of application.yml is required:
spring: application: name: eureka-provider #Service name eureka: client: service-url: defaultZone: http://localhost:2001/eureka/ #Service center address instance: prefer-ip-address: true instance-id: ${spring.application.name}:${server.port}server: port: 2004We also need to add the following tag @EnableEurekaClient to the startup class, which means starting the eureka client, because the service provider belongs to the client compared to the service center; when running the eureka_provider project, we can see the following information in the registration center:
In order to ensure that the service provider interface is fine, we can directly click eureka-provider:2004, and then increase the path to the interface to be used. Here is: http://192.168.153.148:2004/msg, and we can get the following information returned by the normal access interface:
Consumer Discovery Service
With interface services, we also need to consume services, so we create the module project eureka_consumer, because the fegin pseudo-client method is used to access our service provider, and we also need to introduce eureka's dependencies:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId></dependency>
Then define the UserService service at the service layer and implement the interface in the public interface module eureka_api, the code is as follows:
@FeignClient(value = "eureka-provider") public interface UserService extends UserInterface { }Through FeignClient, specify the called server application name eureka-provider. This name corresponds to the application directory registered in the service center. Create a response output UserController at the Controller layer and provide two displayed interfaces, codes such as:
@RestControllerpublic class UserController{ @Autowired private UserService userService; @GetMapping("/users") public MoRp<List<MoUser>> getUsers(MoRq rq) { return userService.getUsers(rq); } @GetMapping("/msg") public String getMsg() { return userService.getMsg(); }}Similarly, the Consumer side also needs to configure some information in application.yml:
spring: application: name: eureka-consumereureka: client: service-url: defaultZone: http://localhost:2001/eureka/ #Register center address instance: prefer-ip-address: true instance-id: ${spring.application.name}:${server.port}server: port: 2005The configuration is similar to that of the provider. Finally, you need to declare the following annotations in the startup class:
@SpringBootApplication@EnableDiscoveryClient //Consumer client@EnableFeignClients //feign client public class EurekaConsumerApplication { public static void main(String[] args) { SpringApplication.run(EurekaConsumerApplication.class, args); }}After starting the eureka_consumer project, we can see the information it has registered in the registration center:
Then, by accessing the eureka_consumer consumer interface, test the interface data of the eureka_provider service provider to see if it can respond normally. The interface address is http:// 192.168.153.148:2005/msg:
The result of the provider is obtained by accessing the consumer, which is the basic testing process for service registration and discovery; as for how the consumer requests the provider interface, we can solve it through the following manual diagram:
Eureka Service Center Highly Available
Judging from the above manual drawing, the service center plays a very important role. Usually, this kind of service center not only builds one, so it is necessary to build a set of highly available service centers; in fact, it is very simple that the configuration of provider and consumer does not need to be done. We only need to configure it in the application.yml of the eureka-server project on the first node and start several services with different ports (the same server has multiple ports, and different server ports may be the same):
server: port: 2001spring: application: name: eureka-serveureureka: client: register-with-eureka: true # When configuring high availability, you need to open and register yourself fetch-registry: true service-url: defaultZone: http://localhost:2002/eureka/ #Register in eureka on port 2002# defaultZone: http://localhost:2001/eureka/ instance: prefer-ip-address: true instance-id: ${spring.application.name}:${server.port} server: eviction-interval-timer-in-ms: 2000 #Exclude failed service intervalsThe following points should be paid attention to in highly available configurations:
Here I created two registry addresses: http://localhost:2001/ and http://localhost:2002/; since the registry addresses configured by provider and consumer are both port 2001, in order to verify high availability, we need to access the 2002 port registration center, the effects are as follows:
You can see that the 2002 port has the same registration information as 2001 port. When I close the application of 2001 port, I can still find the information of provider and consumer in 2002. For more detailed configuration, please refer to the official website description.
git address: https://github.com/shenniubuxing3
nuget release package: https://www.nuget.org/profiles/shenniubuxing3
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.