Eureka is the solution to this problem. In the end, when the consumer calls the provider, the provider’s address is hardcoded in the code


Create the Eureka service


1. We will create a new project cloud-Demo-Eureka on the basis of the original project

This project depends on

<? xml version="1.0" encoding="UTF-8"? > <project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>spring-cloud-demo</artifactId> <groupId>cn.org.zhixiang</groupId> < version > 0.0.1 - the SNAPSHOT < / version > < / parent > < modelVersion > 4.0.0 < / modelVersion > <artifactId>cloud-demo-eureka</artifactId> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> < project. Reporting. OutputEncoding > utf-8 < / project. Reporting. OutputEncoding > < Java version > 1.8 < / Java version > </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <! Note that the dependency here is specific to SpringBoot2.0, If you are using a SpringBoot version earlier than 2.0, use the spring-cloud-starter-eureka-server--> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>Copy the code


2. Create application. Yml

spring:
  application:
    name: eureka-server
# Enable permission authentication
  security: 
    basic:
      enabled: true
    user:
      name: root
      password: root

server:
  host: localhost
  port: 8761
eureka:
  client:
    # This project is not registered as a client
    register-with-eureka: false
    fetch-registry: false
    service-url:
      Eureka address is username: password @address: port number. If permission authentication is not enabled, address: port number is directly used
      defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${server.host}:${server.port}/eureka

Copy the code


3. Create the cn.org.zhixiang package to create the CloudDemoEureApplication startup class

package cn.org.zhixiang; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class CloudDemoEureApplication { public static void main(String[] args) { SpringApplication.run(CloudDemoEureApplication.class, args); }}Copy the code

4. One more operation is required if you have enabled permission verification and SpringBoot version 2.0 or later. If not, you can ignore it

Since CSRF is enabled by default in 2.0, if we directly start Eureka service now, the client cannot be registered, so we need to close CSRF

Create a new Security package under the cn.org.zhixiang package and a new WebSecurityConfigurer class

@EnableWebSecurity public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable(); super.configure(http); }}Copy the code


5. In CloudDemoEureApplication start classes start Eureka services, the browser to http://localhost:8761/eureka, enter the username root and root password after log in Eureka Eureka services to create success.

Modifying the service provider


1. Add the Eureka dependency to the original Cloud-Demo-Provider project

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
Copy the code


2. Modify application.yml and add the following configuration

eurekaServer:
  host: localhost
  port: 8761
  user: root
  password: root

eureka:
  client:
    Register this project with Eureka service
    register-with-eureka: true
    service-url:
      defaultZone: http://${eurekaServer.user}:${eurekaServer.password}@${eurekaServer.host}:${eurekaServer.port}/eureka
Copy the code


3. Add an annotation in CloudDemoProviderApplication start classes: @ EnableEurekaClient labeling is the project had been to the client


4. Then start the Eureka service created just now and start this project. When we visit Eureka again, we can find that our project has been registered with Eureka



5. Since we need to play something more advanced this time, it is definitely not possible to have only one service provider. Let’s copy the above cloud-Demo-Provider project, which is named cloud-Demo-provider-2.

First, the project ID in the POM file is not the same as the last project. It is recommended to call it cloud-demo-provider-2.

The second is that the name of the spring. Application. Name in the yML file should be the same as the name of the previous project: provider-demo. This is not changed, just need to change server.port, to ensure that the port does not conflict, for example I changed to 8079

The third one is UserController. You can see that the User returned in the last project is hard-coded and called Joe. In this project, we change the Joe to Joe to distinguish the two projects


After the modification is complete, the startup project Eureka should look like this

The provider-demo service has two providers


Modify consumer


We have now started to fix the last service address hardcoding problem


1. Added Eureka dependency to cloud-Demo-Consumer project

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
Copy the code

2. Added two annotations to the startup class

@ SpringBootApplication @ EnableEurekaClient public class CloudDemoConsumerApplication {@ Bean @ LoadBalanced / / open load balancing public RestTemplaterestTemplate() {returnnew RestTemplate(); } public static void main(String[] args) { SpringApplication.run(CloudDemoConsumerApplication.class, args); }}Copy the code


3. Make a change in UserController

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/getUser/{id}")
    public User getUser(@PathVariable Long id){
         return restTemplate.getForObject("http://provider-demo/user/getUser/"+id,User.class); }}Copy the code

As you can see, the last time we use localhost: 8078 / user/getUser has been replaced by the provider – demo/user/getUser. I don’t know if you remember this provider-demo? In Eureka, it corresponds to a service ID. That is, in Eureka, we can access the service using IP instead of using IP + port.


Everyone start up after the four projects, visit http://localhost:8088/user/getUser/5 to find a will return to zhang, a ride back to li si. This is the @loadBalanced annotation we added at the beginning, which enables Eureka load balancing. In this case, have we completed yesterday’s problems perfectly?


GitHub:github.com/shiyujun/sp…

Yards cloud: gitee.com/zhixiang_bl…


If it helps you, please remember to help click a star oh


This article is from zhixiang.org.cn/#/blog/read… , please reserve the reprint.