微服务--1.微服务场景模拟

新建一个springboot项目(D:/idea2/cloud-project)
在这个大项目下新建4个maven项目
分别为:user-service-project;user-consumer-project;user-conmmen-project;eureka-server-project
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
代码如下:
user-service-project;
livia.mapper.Countrymapper

package livia.mapper;

import livia.model.Country;
import tk.mybatis.mapper.common.Mapper;

/**
 * @program: cloud-demo-project
 * @description
 * @author: livia
 * @create: 2019-12-26 10:36
 **/
//Country是对应的数据库的名字,实体类也是Country
public interface Countrymapper extends Mapper<Country> {
}

livia.service.CountryService

package livia.service;

import livia.model.Country;

import java.util.List;

/**
 * @program: cloud-demo-project
 * @description
 * @author: livia
 * @create: 2019-12-26 10:33
 **/
public interface CountryService {
    public List<Country> queryAll();
}

livia.service.CountryServiceImpl

package livia.service;


import livia.mapper.Countrymapper;
import livia.model.Country;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: cloud-demo-project
 * @description
 * @author: livia
 * @create: 2019-12-26 10:34
 **/
@Service
public class CountryServiceImpl implements CountryService{
    @Autowired
    private Countrymapper countrymapper;
    public List<Country> queryAll() {
        List<Country> countries = countrymapper.selectAll();
        return countries;
    }
}

livia.web.UserController

package livia.web;

import livia.model.Country;
import livia.service.CountryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: cloud-demo-project
 * @description
 * @author: livia
 * @create: 2019-12-26 13:52
 **/
@RestController
@RequestMapping("/")
public class UserController {
    @Autowired
    CountryService countryService;

    @RequestMapping("findall")
    public String findAll(){
        List<Country> countries = countryService.queryAll();
        StringBuilder sb = new StringBuilder();
        for (Country country: countries){
            sb.append(country.toString());
        }
        System.out.println(sb.toString());
        return sb.toString();
    }
}

livia.Userapplication

package livia;

/**
 * @program: cloud-demo-project
 * @description
 * @author: livia
 * @create: 2019-12-26 10:09
 **/



import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;

/**
 * @SpringBootApplication = (默认属性)@Configuration + @EnableAutoConfiguration + @ComponentScan
 * 1、@Configuration的注解类标识这个类可以使用Spring IoC容器作为bean定义的来源。
 * 2、@EnableAutoConfiguration:能够自动配置spring的上下文,试图猜测和配置你想要的bean类,通常会自动根据你的类路径和你的bean定义自动配置。
 * 3、@ComponentScan:会自动扫描指定包下的全部标有@Component的类,并注册成bean,当然包括@Component下的子注解@Service,@Repository,@Controller。
 */
@SpringBootApplication
@MapperScan("livia.mapper")//通过使用@MapperScan可以指定要扫描的Mapper类的包的路径
public class Userapplication {
    public static void main(String[] args) {
        SpringApplication.run(Userapplication.class,args);
    }
}

application.yml

server:
  port: 8080
  servlet:
      context-path: /
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/frame?characterEnconding=utf-8&serverTimezone=UTC
    username: root
    password: lwy092721..
    driver-class-name: com.mysql.cj.jdbc.Driver
  application:
    name: user-service


mybatis:
  type-aliases-package: livia.model
  configuration:
    map-underscore-to-camel-case: true
eureka:
  client:
    service-url:
      defaultZone: http://localhost:10086/eureka
  instance:
    prefer-ip-address: true
    ip-address: 127.0.0.1

D:\idea2\cloud-project\user-service-project\pom.xml

<?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>demo</artifactId>
        <groupId>com.livia</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>user-service-project</artifactId>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency> <!--mysql-->

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency> <!--mapper启动器 -->

        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.0.3</version>
        </dependency>


        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.1.2.RELEASE</version><!--服务器有可不写-->
        </dependency>
        <dependency>
            <groupId>com.livia</groupId>
            <artifactId>user-conmmen-project</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>


</project>

user-consumer-project;
livia.controller.ConsumerController

package livia.controller;

import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @program: cloud-project
 * @description
 * @author: livia
 * @create: 2019-12-26 15:47
 **/

@RestController
@RequestMapping("/")
public class ConsumerController {
    private String SERVICEURL = "http://localhost:1225/findall";
    @Autowired
    RestTemplate restTemplate;
    @Autowired
    DiscoveryClient discoveryClient;
    @Autowired
    LoadBalancerClient loadBalancerClient;
    @RequestMapping("findall")
    public String findAll(){
//        return restTemplate.getForObject(SERVICEURL,String.class);
        List<ServiceInstance> instances = discoveryClient.getInstances("user-service");
        ServiceInstance instance = instances.get(0);
        String host = instance.getHost();
        int port = instance.getPort();
        String urlByEureka = "http://" + host + ":" + port +"/findall";
        return restTemplate.getForObject(urlByEureka,String.class);
    }
    @RequestMapping("findall2")
    public String findAll2(){
        ServiceInstance instans = loadBalancerClient.choose("user-service");
        String host = instans.getHost();
        int port = instans.getPort();
        String urlByEureka = "http://" + host + ":" + port + "/findall";
        return restTemplate.getForObject(urlByEureka,String.class);
    }
//     @GetMapping("findall3")
//    //    user-service负载均衡加上之后才可以用
//    public String findAll3(){
//        return restTemplate.getForObject("http://user-service/findall",String.class);
//    }

}



livia.ConsumerApplication

package livia;



import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;


/**
 * @program: cloud-demo-project
 * @description
 * @author: livia
 * @create: 2019-12-26 14:38
 **/
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@EnableDiscoveryClient
public class ConsumerApplication {
    public static void main(String[] args) {
       SpringApplication.run(ConsumerApplication.class,args);
    }
    @Bean
    //@LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

application.yml

spring:
  application:
    name: user-consumer-project
server:
  port: 1111
eureka:
  client:
    service-url:
      defaultZone: http://localhost:10086/eureka

D:\idea2\cloud-project\user-consumer-project\pom.xml

<?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>demo</artifactId>
        <groupId>com.livia</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>user-consumer-project</artifactId>
    <dependencies>
        <dependency>
            <groupId>com.netflix.eureka</groupId>
            <artifactId>eureka-client</artifactId>
            <version>1.9.12</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-commons</artifactId>
            <version>2.1.2.RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.1.2.RELEASE</version><!--服务器有可不写-->
        </dependency>
    </dependencies>


</project>

user-conmmen-project;
livia.model.Country

package livia.model;

import org.apache.ibatis.type.JdbcType;
import tk.mybatis.mapper.annotation.ColumnType;

import javax.persistence.Id;

/**
 * @program: cloud-project
 * @description
 * @author: livia
 * @create: 2019-12-26 15:39
 **/
public class Country {
    private static final long serialVersionUID = 6569081236403751407L;
    @Id
    @ColumnType(jdbcType = JdbcType.BIGINT)
    private Long id;
    private String countryname;
    private String countrycode;

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getCountryname() {
        return countryname;
    }

    public void setCountryname(String countryname) {
        this.countryname = countryname;
    }

    public String getCountrycode() {
        return countrycode;
    }

    public void setCountrycode(String countrycode) {
        this.countrycode = countrycode;
    }

    @Override
    public String toString() {
        return "Country{" +
                "id=" + id +
                ", countryname='" + countryname + '\'' +
                ", countrycode='" + countrycode + '\'' +
                '}';
    }
}

D:\idea2\cloud-project\user-conmmen-project\pom.xml

<?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>demo</artifactId>
        <groupId>com.livia</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>user-conmmen-project</artifactId>
    <dependencies>
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
            <version>1.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-core</artifactId>
            <version>1.0.4</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>


</project>

eureka-server-project
livia.eureka.EurekaServerApplication

package livia.eureka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**
 * @program: cloud-project
 * @description
 * @author: livia
 * @create: 2019-12-26 15:55
 **/
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@EnableEurekaServer

public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class,args);
    }
}

application.yml

server:
  port: 10086

eureka:
  client:
    service-url:
      defaultZone: http://localhost:10086/eureka
  instance:
    prefer-ip-address: true
    ip-address: 127.0.0.1
spring:
  application:
    name: eureka-server
  main:
    allow-bean-definition-overriding: true

D:\idea2\cloud-project\eureka-server-project\pom.xml

<?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>demo</artifactId>
        <groupId>com.livia</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>eureka-server-project</artifactId>

    <dependencies>
<!--        @EnableEurekaServer-->
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            <version>2.0.1.RELEASE</version>
        </dependency>

        <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

运行顺序:eureka-server-project
user-service-project
user-consumer-project
验证:(1)http://localhost:8080/findall
在这里插入图片描述
(2)http://localhost:1111/findall3
在这里插入图片描述
(3)http://localhost:10086
在这里插入图片描述


在这中间可能会遇到一些问题:


查看错误和解决办法:点击这里

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值