1.父工程pom.xml如下:
4.0.0 org.example spring boot-demon2pom 1.0-SNAPSHOT eureka-instance eureka-center eurekaClient org.springframework.boot spring-boot-starter-parent2.4.3 org.springframework.boot spring-boot-devtoolstrue org.springframework.boot spring-boot-starter-weborg.springframework.cloud spring-cloud-dependencies2020.0.1 pom import org.springframework.boot spring-boot-maven-plugintrue common.startMain org.apache.maven.plugins maven-compiler-plugin1.8 1.8
2.注册中心
(1)pom.xml如下:
spring boot-demon2 org.example 1.0-SNAPSHOT 4.0.0 eureka-centerorg.springframework.cloud spring-cloud-starter-netflix-eureka-serverorg.springframework.boot spring-boot-starter-security
(2)配置文件如下(application.yml)
spring:
application:
name: eureka-center
#安全认证的登录账号
security:
user:
password: 123456
name: admin
server:
port: 8889
eureka:
client:
#注册中心不需要将自己当作客户端注册
register-with-eureka: false
#注册中心的职责是维护服务的信息等,不需要去检索相关的服务
fetch-registry: false
service-url:
defaultZone: http://localhost:${server.port}/eureka/
#关闭自我保护模式
server:
enable-self-preservation: false
(3)权限类配置bean如下:
package com.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
//开启权限认证
@EnableWebSecurity
public class SercurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
//关闭csrf
http.csrf().disable();
//任何请求都需要进行权限的校验包括基本请求
http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
}
}
(4)启动类
package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurekaServiceMain {
public static void main(String agrs[]){
SpringApplication.run(EurekaServiceMain.class,agrs);
}
}
3.服务提供者
(1)pom.xml:
spring boot-demon2 org.example 1.0-SNAPSHOT 4.0.0 eureka-instanceorg.springframework.cloud spring-cloud-starter-netflix-eureka-clientorg.springframework.boot spring-boot-starter-web
(2)配置文件如下:
server:
port: 8085
spring:
application:
name: serviceProvider
eureka:
client:
service-url:
defaultZone: http://admin:123456@localhost:8889/eureka/
instance:
#采用ip注册
prefer-ip-address: true
#定义实列ID格式
#${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
(3)对外提供接口:
package com.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class helloHandler {
@GetMapping("/hello")
public String getMessage(){
return "你好,世界!";
}
}
(4)启动类:
package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class eurekaServiceProvider {
public static void main(String[] args) {
SpringApplication.run(eurekaServiceProvider.class,args);
}
}
4.消费者
(1)pom.xml配置如下:
spring boot-demon2 org.example 1.0-SNAPSHOT 4.0.0 eurekaClientorg.springframework.cloud spring-cloud-starter-netflix-eureka-client
(2)配置文件(application.yml)
server:
port: 8881
spring:
application:
name: client
eureka:
client:
service-url:
defaultZone: http://admin:123456@localhost:8889/eureka/
instance:
prefer-ip-address: true
instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
(3)RestTemplate配置:
package com.Config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class BeanConfig {
@Bean
//负载均衡
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
(4)服务调用
package com.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class helloService {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/client/hello")
public String getString(){
// return restTemplate.getForObject("http://localhost:8085/hello", String.class);
return restTemplate.getForObject("http://serviceProvider/hello", String.class);
}
}
(5)启动类
package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class ClientMain {
public static void main(String[] args) {
SpringApplication.run(ClientMain.class,args);
}
}



