Learn a little springcloud every day. (b): Service Registration and Discovery Eureka

Source: Internet
Author: User

I believe I've seen it every day. Springcloud (a): The simple service provider consumer calls the students have found that in the end of the consumer call provider when the address of the provider hard coded in the code, this way is certainly not possible, today, we are going to Eureka to solve the problem

Create a Eureka Service

1. We create a new project based on the original project Cloud-demo-eureka

The dependency of this project is

<?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-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> <!--notice here Dependencies are SpringBoot2.0 later, if you are using a springboot version less than 2.0 please use spring-cloud-starter-eureka-server--> <artifactId> spring-cloud-starter-netflix-eureka-server</artifactid> </dependency> <dependency> <grou Pid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-security</artifactid > </dependency></dependencies><build> <plugins> <plugin> <grou Pid>org.springframework.boot</groupid> <artifactid>spring-boot-Maven-plugin</artifactid> </plugin> </plugins></build> 

</project>

2. Create Application.yml

Spring
Application:
Name:eureka-server
#开启权限认证
Security
Basic
Enabled:true
User
Name:root
Password:root

Server
Host:localhost
port:8761
Eureka
Client
#此项目不作为客户端注册
Register-with-eureka:false
Fetch-registry:false
Service-url:
#开启权限验证后Eureka地址为 User name: password @ Address: port number, if not turned on the right to verify the direct use of the address: port number
Defaultzone:http://${spring.security.user.name}:${spring.security.user.password}@${server.host}:${server.port} /eureka

3. Create the Cn.org.zhixiang package, create the Clouddemoeureapplication startup class under this package

Package Cn.org.zhixiang;

Import org.springframework.boot.SpringApplication;
Import org.springframework.boot.autoconfigure.SpringBootApplication;
Import Org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplicationbr/> @EnableEurekaServer

public static void Main (string[] args) {
Springapplication.run (Clouddemoeureapplication.class, args);
}
}

4. If you have permission verification turned on and the Springboot version is more than 2.0, you will need an action if it is not this cloth can be ignored

Because the 2.0 default on CSRF, if we start the Eureka service now directly, the client is not registered, so we need to turn off the CSRF

Create a new security package under the Cn.org.zhixiang package and create a new Websecurityconfigurer class

@EnableWebSecurity
public class Websecurityconfigurer extends Websecurityconfigureradapter {br/> @Override

HTTP.CSRF (). disable ();
Super.configure (HTTP);
}
}

5. Start the Eureka service in the Clouddemoeureapplication startup class, the browser accesses Http://localhost:8761/eureka, Enter the username root and password root login Eureka after the Eureka Service was created successfully.

Modify a service Provider

1. Add Eureka dependencies to the original Cloud-demo-provider project

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

2. Modify the Application.yml to add the following configuration

Eurekaserver:
Host:localhost
port:8761
User:root
Password:root

Eureka
Client
#将此项目注册到Eureka服务
Register-with-eureka:true
Service-url:
defaultzone:http://${eurekaserver.user}:${eurekaserver.password}@${eurekaserver.host}:${eurekaserver.port}/ Eureka

3. Add an annotation to the Clouddemoproviderapplication startup class: @EnableEurekaClient indicate that this item is Eureka Client

4. Then start this project by starting the Eureka service you just created, and when we visit Eureka again we can see that our project has been registered Eureka

5. Because we need to play a bit bigger this time, so only a service provider is certainly not, we copy the above Cloud-demo-provider project, the new project named Cloud-demo-provider-2, There are only three places for this new project to be modified:

The first is the project ID in the Pom file note not as in the previous project, the recommendation is directly called Cloud-demo-provider-2.

The second is the yml file spring.application.name should be the same as the previous project, named: Provider-demo. This does not modify, only need to modify the Server.port, to ensure that the port does not conflict, such as I changed to 8079

The third is Usercontroller, you can see the last item returned by the user is hard-coded called Zhang San, this project we changed this Zhang San to John Doe, to distinguish between the two projects

Start the project after the modification is complete Eureka should be like this

That's what this Provider-demo service has meant for two providers.

Modify Consumer

Now that we've started to modify the last hard-coded service address,

1.cloud-demo-consumer Project increases Eureka Dependency

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
2. Two annotations added to the startup class

@SpringBootApplicationbr/> @EnableEurekaClient
@Bean

Public Resttemplate resttemplate () {
return new Resttemplate ();
}
public static void Main (string[] args) {
Springapplication.run (Clouddemoconsumerapplication.class, args);
}
}

3. Make a change in Usercontroller

@RestControllerbr/> @RequestMapping ("/user")
< p="">

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

}
As you can see, the localhost:8078/user/getuser we used last time has been replaced by Provider-demo/user/getuser. I wonder if you remember this provider-demo? It is the attribute value of Spring.application.name in two projects, in Eureka, it corresponds to the ID of a service, that is to say, in Eureka, we can use IP to access the service instead of using the ip+ port.

After all the four projects have been started, access to HTTP://LOCALHOST:8088/USER/GETUSER/5, is not found to return to Zhang San, one will return to John Doe it. This is the @loadbalanced annotation we started with, that is, Eureka load balancing is turned on. In this way, have we finished the problems left over yesterday?

Github:https://github.com/2388386839/spring-cloud-demo

Code Cloud: Https://gitee.com/zhixiang_blog/apring-cloud-demo

If you have any help, please remember to help a star oh.

This article is from HTTPS://ZHIXIANG.ORG.CN/#/BLOG/READ/3E9E73FD-BDA0-4638-826B-CCEB43B77B07, reproduced please retain.

Learn a little springcloud every day. (b): Service Registration and Discovery Eureka

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.